package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.*;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrdersMapper;
import com.sky.result.PageResult;
import com.sky.service.OrdersService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class OrdersServiceimpl implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Override
    @ApiOperation("分页查询")
    public PageResult page(OrdersPageQueryDTO ordersPageQueryDTO) {
        PageHelper.startPage(ordersPageQueryDTO.getPage(), ordersPageQueryDTO.getPageSize());
        // 获取订单
        Page<OrderVO> page = ordersMapper.page(ordersPageQueryDTO);
        // 获取订单详情
        List<OrderVO> result = page.getResult();
        for (int i = 0; i < result.size(); i++) {
            List<OrderDetail> orderDetailList = orderDetailMapper.queryById(result.get(i).getId());
            if (orderDetailList != null && orderDetailList.size() != 0) {
                String dish = "";
                for (OrderDetail orderDetail : orderDetailList) {
                    dish = dish + orderDetail.getName() + "*" + orderDetail.getNumber() + ";";
                }
                result.get(i).setOrderDishes(dish);
            }
        }
        return new PageResult(page.getTotal(), result);
    }

    @Override
    @ApiOperation("各个状态的订单数量统计")
    public OrderStatisticsVO count() {
        // 待接单数量
        Integer toBeConfirmed = ordersMapper.count(Orders.TO_BE_CONFIRMED);
        // 待派送数量
        Integer confirmed = ordersMapper.count(Orders.CONFIRMED);
        // 派送中数量
        Integer deliveryInProgress = ordersMapper.count(Orders.DELIVERY_IN_PROGRESS);
        return new OrderStatisticsVO(toBeConfirmed,confirmed,deliveryInProgress);
    }


    @Override
    @ApiOperation("拒单")
    public void update(OrdersRejectionDTO ordersRejectionDTO) {
        ordersMapper.update(Orders.builder()
                .id(ordersRejectionDTO.getId())
                .status(Orders.CANCELLED)
                .cancelTime(LocalDateTime.now())
                .cancelReason(ordersRejectionDTO.getRejectionReason())
                .build());

    }

    @Override
    @ApiOperation("接单")
    public void pas(OrdersConfirmDTO ordersConfirmDTO) {
        ordersMapper.update(Orders.builder().id(ordersConfirmDTO.getId()).status(Orders.CONFIRMED).build());

    }

    @Override
    @ApiOperation("取消订单")
    public void statusDele(OrdersCancelDTO ordersCancelDTO) {
        ordersMapper.cancellation(Orders.builder()
                .id(ordersCancelDTO.getId())
                .status(Orders.CANCELLED)
                .cancelTime(LocalDateTime.now())
                .rejectionReason(ordersCancelDTO.getCancelReason()).build());

    }

    @Override
    @ApiOperation("派送订单")
    public void statusCome(Integer id) {
        ordersMapper.cancellation(Orders.builder()
                .id(Long.valueOf(id))
                .status(Orders.DELIVERY_IN_PROGRESS)
                .build());
    }

    @Override
    @ApiOperation("完成订单")
    public void statusComplete(Integer id) {
        ordersMapper.cancellation(Orders.builder()
                    .id(Long.valueOf(id))
                    .status(Orders.COMPLETED)
                    .deliveryTime(LocalDateTime.now())
                    .build());
    }

    @Override
    public OrderVO details(Long id) {
        // 获取订单
        OrderVO query = ordersMapper.detail(Orders.builder().id(Long.valueOf(id)).build());
        // 获取订单详情
        query.setOrderDetailList(orderDetailMapper.queryById(query.getId()));
        return query;
    }

}
